/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.server.settings;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import jeeobserver.JvmObserver;
import jeeobserver.utilities.SizeUtilities;

/**
 * The Class JvmNotificationRule.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public final class JvmNotificationRule extends JvmExclusionRule {

	 /** The Constant serialVersionUID. */
	 private static final long serialVersionUID = 40L;

	 /** The Constant TAG_CODE_CONTEXT. */
	 public static final String TAG_CODE_CONTEXT = "{context}";

	 /** The Constant TAG_CODE_CPU_USAGE_PERCENTAGE. */
	 public static final String TAG_CODE_CPU_USAGE_PERCENTAGE = "{cpu_usage_percentage}";

	 /** The Constant TAG_CODE_HEAP_MEMORY_USED_BYTES. */
	 public static final String TAG_CODE_HEAP_MEMORY_USED_BYTES = "{heap_memory_used_bytes}";

	 /** The Constant TAG_CODE_HEAP_MEMORY_USED_FORMATTED. */
	 public static final String TAG_CODE_HEAP_MEMORY_USED_FORMATTED = "{heap_memory_used_formatted}";

	 /** The Constant TAG_CODE_NON_HEAP_MEMORY_USED_BYTES. */
	 public static final String TAG_CODE_NON_HEAP_MEMORY_USED_BYTES = "{non_heap_memory_used_bytes}";

	 /** The Constant TAG_CODE_NON_HEAP_MEMORY_USED_FORMATTED. */
	 public static final String TAG_CODE_NON_HEAP_MEMORY_USED_FORMATTED = "{non_heap_memory_used_formatted}";

	 /** The first element in rule time map. */
	 private static Map<String, Long> firstElementInRuleTimeMap = new HashMap<String, Long>();

	 /** The element notificated set. */
	 private static Set<String> elementNotificatedSet = new HashSet<String>();

	 /** The notification. */
	 private Notification notification;

	 /** The send notification only once. */
	 private boolean sendNotificationOnlyOnce;

	 /** The limit time period. */
	 private TimePeriod limitTimePeriod;

	 public boolean isObserverInRule(JvmObserver observer) {

			boolean result = super.iObserverInRule(observer);

			final String idUnique = this.getName() + "[" + observer.getContext() + "]";

			if (result == false) {
				 JvmNotificationRule.elementNotificatedSet.remove(idUnique);
				 JvmNotificationRule.firstElementInRuleTimeMap.remove(idUnique);
			}

			// Check duration
			if (result == true) {
				 if (!JvmNotificationRule.firstElementInRuleTimeMap.containsKey(idUnique)) {
						JvmNotificationRule.firstElementInRuleTimeMap.put(idUnique, new Long(observer.getDate().getTime()));
				 }

				 final long firstElementMillis = JvmNotificationRule.firstElementInRuleTimeMap.get(idUnique).longValue();

				 if (((observer.getDate().getTime() + (observer.getTimeNanos() / new Long(1000000).longValue())) - firstElementMillis) < this.getLimitTimePeriod().getMillis()) {
						result = false;
				 }
			}

			if (result == true) {
				 if (this.sendNotificationOnlyOnce == true) {
						if (JvmNotificationRule.elementNotificatedSet.contains(idUnique)) {
							 result = false;
						}
				 }
				 JvmNotificationRule.elementNotificatedSet.add(idUnique);
			}

			if (result == true) {
				 JvmNotificationRule.firstElementInRuleTimeMap.remove(idUnique);
			}

			return result;

	 }

	 /**
	  * Instantiates a new jvm notification rule.
	  *
	  * @param notification the notification
	  */
	 /**
	  * Instantiates a new jvm notification rule.
	  *
	  * @param notification
	  * the notification
	  */
	 public JvmNotificationRule(Notification notification) {
			this.notification = notification;
	 }

	 /**
	  * Gets the notification.
	  *
		* @param observer
	  *
	  * @return the notification
	  */
	 public Notification getNotification(JvmObserver observer) {

			if (observer != null) {
				 this.notification.setTag(JvmNotificationRule.TAG_CODE_CONTEXT, observer.getContext());

				 final DecimalFormat percentageDecimalFormat = new DecimalFormat("#0.00 %");

				 this.notification.setTag(JvmNotificationRule.TAG_CODE_CPU_USAGE_PERCENTAGE, percentageDecimalFormat.format(observer.getCpuUsagePercentage()));
				 this.notification.setTag(JvmNotificationRule.TAG_CODE_HEAP_MEMORY_USED_BYTES, SizeUtilities.fromBytesToString(observer.getHeapMemoryUsedBytes()));
				 this.notification.setTag(JvmNotificationRule.TAG_CODE_HEAP_MEMORY_USED_FORMATTED, SizeUtilities.fromBytesToString(observer.getHeapMemoryUsedBytes()));

				 this.notification.setTag(JvmNotificationRule.TAG_CODE_NON_HEAP_MEMORY_USED_BYTES, SizeUtilities.fromBytesToString(observer.getNonHeapMemoryUsedBytes()));
				 this.notification.setTag(JvmNotificationRule.TAG_CODE_NON_HEAP_MEMORY_USED_FORMATTED, SizeUtilities.fromBytesToString(observer.getNonHeapMemoryUsedBytes()));
			}
			return this.notification;
	 }

	 /**
	  * Gets the limit time period.
	  *
	  * @return the limit time period
	  */
	 public TimePeriod getLimitTimePeriod() {
			return this.limitTimePeriod;
	 }

	 /**
	  * Sets the limit time period.
	  *
	  * @param limitTimePeriod the new limit time period
	  */
	 public void setLimitTimePeriod(TimePeriod limitTimePeriod) {
			this.limitTimePeriod = limitTimePeriod;
	 }

	 /**
	  * Gets the notification.
	  *
	  * @return the notification
	  */
	 public Notification getNotification() {
			return this.notification;
	 }

	 /**
	  * Sets the notification.
	  *
	  * @param notification the new notification
	  */
	 public void setNotification(Notification notification) {
			this.notification = notification;
	 }

	 /**
	  * Checks if is send notification only once.
	  *
	  * @return true, if is send notification only once
	  */
	 public boolean isSendNotificationOnlyOnce() {
			return this.sendNotificationOnlyOnce;
	 }

	 /**
	  * Sets the send notification only once.
	  *
	  * @param sendNotificationOnlyOnce the new send notification only once
	  */
	 public void setSendNotificationOnlyOnce(boolean sendNotificationOnlyOnce) {
			this.sendNotificationOnlyOnce = sendNotificationOnlyOnce;
	 }
}
